<template>
  <div class="form">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="realName">
        <el-input v-model="dataForm.realName"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="dataForm.mobile"></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="dataForm.email"></el-input>
      </el-form-item>
      <!-- <el-form-item label="最近登录" >
        <el-input :value="dataForm.loginInfo&&dataForm.loginInfo.loginTime" disabled></el-input>
      </el-form-item> -->
    </el-form>
    <div class="btn">
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </div>
  </div>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        username: "",
        realName: "",
        email: "",
        mobile: "",
      },
      tempMobile: "",
      tempRealName:''
    };
  },
  computed: {
    dataRule() {
      return {
        realName: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        mobile: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        // email: [
        //   {
        //     required: true,
        //     message: this.$t("validate.required"),
        //     trigger: "blur",
        //   },
        // ],
      };
    },
  },
  created() {
    this.getInfo();
  },
  methods: {
    // 获取信息
    getInfo() {
      this.$http
        .get("/hotel/personal/center/info")
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...res.data,
          };
          console.log(res.data)
          this.tempMobile = this.dataForm.mobile;
          this.tempRealName = this.dataForm.realName;
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          //加密
          let newDataForm = Object.assign({}, this.dataForm);
           if (!newDataForm.realName || this.tempRealName != newDataForm.realName) {
            //编辑了
            newDataForm.realName = this.$CryptoJS.encrypt(newDataForm.realName);
          }else{
            newDataForm.realName=null;
          }
          if (!newDataForm.mobile || this.tempMobile != newDataForm.mobile) {
            //编辑了
            newDataForm.mobile = this.$CryptoJS.encrypt(newDataForm.mobile);
          }else{
            newDataForm.mobile=null;
          }
          this.$http[!newDataForm.id ? "post" : "put"](
            "/hotel/personal/center",
            newDataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
<style lang="scss" scoped>
.form {
  padding: 30px;
  padding-right: 10%;
  background-color: #fff;
}
</style>
